package com.wiserun.RestFul.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wiserun.RestFul.util.Office2Pdf;

import sun.misc.BASE64Decoder;

@Service
public class FileRestService {
	
	@Autowired
	private Office2Pdf o2p;
	
	private static final Logger logger = LoggerFactory.getLogger(FileRestService.class);
	/**
	* @param base64Content
	* @param filePath
	* base64字符串转pdf
	*/
	public   void base64StringToFile(String base64Content, String filePath) {
		BASE64Decoder decoder = new BASE64Decoder();
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			byte[] bytes = decoder.decodeBuffer(base64Content);// base64编码内容转换为字节数组
			ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
			bis = new BufferedInputStream(byteInputStream);
			File file = new File(filePath);
			File path = file.getParentFile();
			if (!path.exists()) {
				path.mkdirs();
			}
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			byte[] buffer = new byte[1024];
			int length = bis.read(buffer);
			while (length != -1) {
				bos.write(buffer, 0, length);
				length = bis.read(buffer);
			}
			bos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		try {
			bos.close();
			fos.close();
			bis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
	}
	public  String encryptToBase64(String filePath) {
		if (filePath == null) {
			return null;
		}
		try {
			byte[] b = Files.readAllBytes(Paths.get(filePath));
			String outString=Base64.getEncoder().encodeToString(b);
			//outString=outString.replaceAll("\r\n","");//替换换行和空格
			//outString=outString.replaceAll("\\+","%2B");//对base64的+转义
			
			return outString;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据文件类型决定如何转换
	 * @param filename 文件名
	 * @param source 原文件路径
	 * @param target 目标文件路径
	 * @return
	 * @throws IOException 
	 */
	public boolean whichFile2PDF(String filename,String source,String target) throws IOException {
		String type = filename.substring(filename.lastIndexOf("."));
		logger.info("whichFile2PDF=>"+type);
		boolean flag = false;
		//由于此功能支持多种格式，因此均使用同一方法
		switch(type){
			case ".doc":
				flag = o2p.office2pdf(source,target);
				break;
			case ".docx":
				flag = o2p.office2pdf(source,target);
				break;
			case ".xls":
				flag = o2p.office2pdf(source,target);
				break;
			case ".xlsx":
				flag = o2p.office2pdf(source,target);
				break;
			case ".ppt":
				flag = o2p.office2pdf(source,target);
				break;
			case ".pptx":
				flag = o2p.office2pdf(source,target);
				break;
			default:
				break;
		}
		return flag;
	}
	
	public boolean deleteTempFile(String filePath,String pdfPath) {
		boolean flag=false;
		File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();// 文件删除
            flag = true;
        }
        File pdf=new File(pdfPath);
        if (pdf.isFile() && pdf.exists()) {
        	pdf.delete();// 文件删除
            flag = true;
        }
		return flag;
	}
}
